package com.pphh.demo.service;

import com.pphh.demo.dao.UserInfoDAO;
import com.pphh.demo.model.UserInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    UserInfoDAO userInfoDAO;

    public UserInfoDTO getByUserId(Long userId) {
        return userInfoDAO.findByUserId(userId);
    }

    @Transactional(value = "txManager", readOnly = true)
    public UserInfoDTO updateUser(UserInfoDTO user) {
        userInfoDAO.insert(user);
        return user;
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public UserInfoDTO insertUser(UserInfoDTO user) throws Exception {
        userInfoDAO.insert(user);
        throw new Exception("函数执行有异常!");
    }

    public UserInfoDTO insertUserNTx(UserInfoDTO user) throws Exception {
        return insertUserInner(user);
    }

    @Transactional(value = "txManager")
    public UserInfoDTO insertUserInner(UserInfoDTO user) throws Exception {
        userInfoDAO.insert(user);
        throw new RuntimeException("函数执行有异常!");
    }

    @Transactional
    public void remove(Long userId) {
        userInfoDAO.deleteByUserId(userId);
    }
}
